package com.java.multipledatasource.ms;


public class NodeMerge {

    public static  Node merge(Node head1, Node head2){
            if (head1 == null) {
                return  head2;
            }
            if (head2 == null) {
                return head1;
            }
            Node head = new Node();
            Node node = new Node();
            head.setNext(node);

            while (head1 != null && head2 != null) {
               if (head1.getData()  <= head2.getData()) {
                   node.setNext(head1);
                   head1 = head1.getNext();
               } else {
                   node.setNext(head2);
                    head2 = head2.getNext();
               }
               node = node.getNext();
            }

            if (head1 != null) {
                node.setNext(head1);
            } else {
                node.setNext(head2);
            }

        return head.getNext().getNext();
    }


    public static void main(String[] args) {
        Node node11  = new Node(1);
        Node node12  = new Node(3);
        Node node13  = new Node(5);
        Node node14  = new Node(7);
        node11.setNext(node12);
        node12.setNext(node13);
        node13.setNext(node14);

        Node node21 = new Node(2);
        Node node24 = new Node(4);
        Node node26 = new Node(6);

        node21.setNext(node24);
        node24.setNext(node26);

        Node resNode = merge(node11, node21);

        while (resNode != null) {
            System.out.println(resNode.getData());
            resNode = resNode.getNext();
        }
    }
}
